package jwt.lab02;

import javax.mail.*;
import java.util.Properties;

public class ShowInbox {

	public static void main(String[] args) throws MessagingException {
		System.out.println("Checking mail...");
		Properties props = Utils.loadProperties(Utils.PROPERTIES_FILE);

		Session session = Session.getDefaultInstance(props, null);

		Store store = session.getStore(props.getProperty("mail.store.protocol"));
		store.connect(props.getProperty("mail.pop3.host"), props.getProperty("mail.pop3.user"), props.getProperty("mail.pop3.password"));

		Folder folder = store.getFolder("INBOX");
		folder.open(Folder.READ_ONLY);
		
		Message message[] = folder.getMessages();
		StringBuffer sb = new StringBuffer();

		for (int i = 0, n = message.length; i < n; i++)
			sb.append(i + ": " + Utils.messageToString(message[i]) + "\n");

		folder.close(false);
		store.close();


		System.out.println(".:[Inbox " + props.getProperty("mail.pop3.user") + "@" + props.getProperty("mail.pop3.host") + "]:.");
		System.out.println(sb.toString());
	}

}
